package com.yichat.micro.msghttpservice.core.record.service;

import com.yichat.micro.msghttpservice.common.result.HttpResult;
import com.yichat.micro.msghttpservice.core.base.AdvancedExecutor;
import com.yichat.micro.msghttpservice.core.retry.RetryMsgHttpRequestVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by chenmohan on 2019/04/22
 *
 * @description
 */
//@Service("asyncRecordServiceImpl")
public class AsyncRecordServiceImpl extends RecordServiceImpl {
    /*
    protected static final ThreadPoolExecutor recordExecutor = AdvancedExecutor.newRecordExecutor();

    protected static ConcurrentHashMap<Long, RetryMsgHttpRequestVo> insertMap = new ConcurrentHashMap<>();


    @Override
    public void recordBeforeResponse(RetryMsgHttpRequestVo retryVo) {
        recordExecutor.submit(() -> {
            super.recordBeforeResponse(retryVo);
        });
    }

    @Override
    public void record(RetryMsgHttpRequestVo retryVo, HttpResult httpResult) {
        recordExecutor.submit(() -> {
            super.record(retryVo, httpResult);
        });
    }

    @Override
    public void recordWithRetryFailed(RetryMsgHttpRequestVo retryVo) {
        if (retryVo.isBatch()) {    //批量处理并发写入数据库时,index会变化,这里会拷贝一份对象写入日志
            RetryMsgHttpRequestVo copy = new RetryMsgHttpRequestVo();
            BeanUtils.copyProperties(retryVo, copy);
            recordExecutor.submit(() -> {
                super.recordWithRetryFailed(copy);
            });
        } else {
            recordExecutor.submit(() -> {
                super.recordWithRetryFailed(retryVo);
            });
        }
    }

    @Override
    public void recordWithSuccessed(RetryMsgHttpRequestVo retryVo, HttpResult httpResult) {
        recordExecutor.submit(() -> {
            super.recordWithSuccessed(retryVo, httpResult);
        });
    }

    @Override
    public void recoredWithBatchFailed(RetryMsgHttpRequestVo retryVo) {
        recordExecutor.submit(() -> {
            super.recoredWithBatchFailed(retryVo);
        });
    }

    public static ThreadPoolExecutor getRecordExecutor() {
        return recordExecutor;
    }
    */
}
